<?php

namespace App\Http\Controllers;

use App\Exceptions\Handler;
use App\Http\Requests\BaseRequest;
use App\services\BaseService;

class BaseController extends Controller
{
    /** @var BaseService */
    protected $services = null;

    /** @var BaseRequest */
    protected $request = null;

    protected $isPage = false;

    protected $servicesName = '';

    protected $isJson = false;

    //
    public function __construct()
    {
        //统一处理request

        //合并后台配置
    }

    public function getServices()
    {
        return $this->services;
    }

    /**
     * 列表入口
     * @return mixed
     */
    public function index()
    {

        return $this->lists();
    }


    /**
     * 删除
     * @return mixed
     */
    public function destroy($id)
    {
        $id = request('id', $id);
        if (empty($id)) {
            return $this->failure("缺少参数1");
        }
        $this->services->setData('id', $id);

        return $this->delete();
    }


    /**
     * 修改
     * @return mixed
     */
    public function update($id)
    {

        $id = \request('id', $id);
        if (empty($id)) {
            return $this->failure("缺少参数1");
        }
        $this->services->setData('id', $id);
        $result = $this->services->update();
        if ($result) {
            return $this->success(null, $this->services->getInfo('成功'));
        } else {
            return $this->failure($this->services->getError());
        }
    }


    /**
     *  保存
     */
    public function store()
    {

        $result = $this->services->save();
        if ($result) {
            return $this->success(null, $this->services->getInfo('成功'));
        } else {
            return $this->failure($this->services->getError());
        }
    }


    /**
     *  删除
     */
    public function delete()
    {
        $result = $this->services->delete();
        if ($result) {
            return $this->success(null, $this->services->getInfo('成功'));
        } else {
            return $this->failure($this->services->getError());
        }
    }


    /**
     *  获取列表信息
     */
    public function lists()
    {

        $data = $this->services->lists($this->isPage);;
        if (false === $data) {
            $message = $this->services->getError('Error');
            return $this->failure($message);
        } else {
            $message = $this->services->getInfo('成功');
            return $this->success($data, $message);
        }
    }


    /**
     *  获取详细信息
     */
    public function detail($id)
    {

        $id = request('id', $id);
        if (empty($id)) {
            return $this->failure("缺少参数1");
        }
        $this->services->setData('id', $id);

        return $this->show();
    }

    /**
     *  获取详细信息
     */
    public function show()
    {
        $data = $this->services->show();
        if (false === $data) {
            $message = $this->services->getError('Error');
            return $this->failure("", $message);
        } else {
            $message = $this->services->getInfo('成功');
            return $this->success($data, $message);
        }
    }


    /**
     * 成功返回
     * @param string $msg
     * @param $data array
     * @param string $url
     * @return \Illuminate\Http\JsonResponse
     */
    protected function success($data = null, $msg = '成功', $url = '')
    {
        return response()->json([
            'code' => Handler::STATUS_NORMAL,
            'msg' => $msg,
            'data' => $data,
            'url' => $url,
            'http_code' => Handler::STATUS_NORMAL
        ], Handler::STATUS_NORMAL, array(), JSON_UNESCAPED_UNICODE);
    }


    /**
     * 失败返回
     * @param $msg string
     * @param $data array
     * @param string $url
     * @param int $status
     * @param int $code
     * @return \Illuminate\Http\JsonResponse
     */
    protected function failure($msg, $data = null, $url = '', $status = Handler::STATUS_NORMAL, $code = Handler::FAIL_CODE)
    {
        return response()->json([
            'code' => $code,
//            'status' => $status,
            'msg' => $msg,
            'data' => $data,
            'http_code' => Handler::STATUS_BAD_REQUEST,
            'url' => $url,
        ], $status, array(), JSON_UNESCAPED_UNICODE);

    }

    /**
     * 下载Excel模板
     * @param $file_url
     * @param string $name
     */
    public function downloadFile($file_url, $name = '')
    {
        if (!file_exists($file_url)) {
            die('查无此模板文件');
        }
        //$file_url = public_path($file_url);
        $file_arr = explode('.', $file_url);
        $file_ext = $file_arr[count($file_arr) - 1];
        $name = empty($name) ? time() . '.' . $file_ext : $name;
        $file = @fopen($file_url, "r");
        ob_end_clean();
        Header('Pragma: public');
        Header('Expires: 0');
        Header('Cache-Control: private');
        Header('Cache-Component: must-revalidate, post-check=0, pre-check=0');
        Header("Content-type: application/octet-stream");
        Header("Content-Disposition: attachment; filename=" . $name);//给文件命名
        Header('Content-Encoding: none');
        Header("Content-Transfer-Encoding: binary");
        /* while (!feof ($file)) {
            echo fread($file,5000);
        } */
        @readfile($file_url);
        fclose($file);
    }

}
